package com.cheetah.start.module.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheetah.start.common.query.PageQuery;
import com.cheetah.start.common.utils.MyPasswordUtil;
import com.cheetah.start.module.system.mapper.SysAdminMapper;
import com.cheetah.start.module.system.service.SysAdminService;
import com.cheetah.start.module.system.domain.SysAdmin;
import com.cheetah.start.module.system.service.SysRoleAdminService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Feel
 * @create 2021/7/18
 **/
@AllArgsConstructor
@Service
public class SysAdminServiceImpl extends ServiceImpl<SysAdminMapper, SysAdmin> implements SysAdminService {

    @Autowired
    private SysRoleAdminService roleAdminService;

    @Override
    public SysAdmin getByName(String name) {
        QueryWrapper<SysAdmin> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        return getOne(wrapper);
    }

    @Override
    public boolean saveWithRoleIds(SysAdmin sysAdmin) {
        save(sysAdmin);
        boolean b = roleAdminService.saveBySysAdmin(sysAdmin);
        return b;
    }


    @Override
    public boolean updateByIdWithRole(SysAdmin sysAdmin) {
        if (StrUtil.isNotBlank(sysAdmin.getPassword())) {
            sysAdmin.setPassword(MyPasswordUtil.hashPassword(sysAdmin.getPassword()));
        } else {
            sysAdmin.setPassword(null);
        }
        boolean b = updateById(sysAdmin);
        roleAdminService.removeByAdminId(sysAdmin.getAdminId());
        roleAdminService.saveBySysAdmin(sysAdmin);
        return b;
    }

    @Override
    public boolean isRepeat(SysAdmin sysAdmin) {
        LambdaQueryWrapper<SysAdmin> wrapper = getQueryWrapper(null);
        wrapper.eq(SysAdmin::getName, sysAdmin.getName());
        if (sysAdmin.getAdminId() != null) {
            wrapper.ne(SysAdmin::getAdminId, sysAdmin.getAdminId());
        }
        List<SysAdmin> list = list(wrapper);
        return CollUtil.isNotEmpty(list);
    }

    @Override
    public Page<SysAdmin> pageList(PageQuery pageQuery, SysAdmin sysAdmin) {
        Page<SysAdmin> pageList = new Page<>(pageQuery.getPage(), pageQuery.getLimit());
        LambdaQueryWrapper<SysAdmin> queryWrapper = getQueryWrapper(sysAdmin);
        page(pageList, queryWrapper);
        return pageList;
    }

    private LambdaQueryWrapper<SysAdmin> getQueryWrapper(SysAdmin sysAdmin) {
        LambdaQueryWrapper<SysAdmin> queryWrapper = Wrappers.lambdaQuery(SysAdmin.class);
        if (sysAdmin != null) {
            queryWrapper.like(StrUtil.isNotBlank(sysAdmin.getName()), SysAdmin::getName, sysAdmin.getName());
            queryWrapper.like(StrUtil.isNotBlank(sysAdmin.getRealName()), SysAdmin::getRealName, sysAdmin.getRealName());
        }
        queryWrapper.orderByDesc(SysAdmin::getAdminId);
        return queryWrapper;
    }
}
